/**
 * Class Page
 * this class manage pages
 */
Page = (function(_super) {

    __extends(Page, _super);
    /**
     * Page constructor
     * @param context {Application} applicaiton instance
     * @constructor
     */
    function Page(context) {
        Page.__super__.constructor.call(this, context);
        this.userIScroll = false;
        this.isCurrent = false;
    }
    /**
     * Set Content view
     * @param view {String|View|HTMLElement} id, view or html element
     */
    Page.prototype.setContentView = function(view){
        Page.__super__.setContentView.call(this, view);
        if(!this.userIScroll) return;
        var scroll = this.view.select(".content", true);
        this.scroll = new iScroll(scroll, {
            hideScrollbar: true
        });
    };
    /**
     * Create and return title instance
     * @param [view] {View|String|HTMLElement} id, view or html element search for .title-bar css class if we have no view
     * @returns {TitleBar} titlebar instance
     */
    Page.prototype.getTitle = function(view) {
       	return this.title = new TitleBar(view || this.view.select('.title-bar', true));
    };
    /**
     * Before Show function
     * @param intent {Intent} intent that we show this element
     */
    Page.prototype.beforeShow = function(intent){
        if(!this.userIScroll) return;
        this.scroll.scrollTo(0, 0);
    };
    /**
     * On Show function
     * @param intent {Intent} intent that we show this element
     */
    Page.prototype.onShow = function(intent){
        var _this = this;
        if(!this.userIScroll) return;
        setTimeout(function(){
            _this.scroll.refresh();
        },1);
        this.isCurrent = true;
    };
    /**
     * On Hide function
     * @param intent {Intent} intent that we hide this element
     */
    Page.prototype.onHide = function(intent){
        this.isCurrent = false;
    };
    /**
     * Get page context
     * @returns {Application}
     */
	Page.prototype.getContext = function(){
		return this.context;
	};
    /**
     * Add new Fragment
     * @param name {String}
     * @param fragment {Fragment}
     */
    Page.prototype.addFragment = function(name, fragment){
        this.fragments = this.fragments || {};
        this.fragments[name] = fragment;
        this.fragments[name].name = name;
    };
    /**
     * Show fragment with intent and set current if we want or not
     * @param intent {Intent} intent that we show this fragment
     * @param current {Boolean} if we want this fragment to be current or not
     */
    Page.prototype.showFragment = function(intent, current){
        var _name = intent.getName(),
			_current = current === void 0 ? true : current,
            _animation = intent.getAnimation();
		if(_current === true){
			this._currentFragment = this.fragments[_name];
		}
        if(this.fragments[_name]){
            this.fragments[_name].setAnimation(_animation);
            this.fragments[_name].show(intent);
        }
    };
    /**
     * Hide fragment with intent
     * @param intent {Intent} intent that we hide this fragment
     */
    Page.prototype.hideFragment = function(intent){
        var _name = intent.getName(),
			_animation = intent.getAnimation(),
            fragment = this.fragments[_name];
        if(fragment && fragment === this._currentFragment) this._currentFragment == null;
        fragment.setAnimation(_animation);
        fragment.hide(intent);
    };
    /**
     * Hide current fragment if we have current
     * @param intent {Intent} intent that we hide current fragment
     */
    Page.prototype.hideCurrentFragment = function(intent){
        if(!this._currentFragment) return;
        if(intent){
            this._currentFragment.setTranslation(intent.getTranslation());
        }

        this._currentFragment.hide(intent);
    };
    /**
     * Get current fragment
     * @returns {Fragment} current fragment
     */
    Page.prototype.getCurrentFragment = function(){
        return this._currentFragment;
    };

    return Page;

})(Base);